package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService commendService;
    @Autowired
    private ITravelLikeService travelLikeService;

    @PostMapping
    public JSONResult save(Travel travel) {
        JSONResult result = new JSONResult();
        try {
            travel.setAuthor(UserContext.getCurrentUser());
            travelService.save(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/{id}")
    public Object get(@PathVariable Long id) {
        return travelService.get(id);
    }

    // 查看所有游记
    @GetMapping
    public Object getAll(TravelQueryObject qo) {
        // 查看已经发布的游记
        qo.setState(Travel.STATE_RELEASE);
        // 查看的游记必须是公开的
        qo.setIsPublic(true);
        // 排序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.query(qo);
    }

    @PutMapping
    public JSONResult update(Travel travel) {
        JSONResult result = new JSONResult();
        try {
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/commends")
    public Object getCommends(@ModelAttribute("qo") TravelCommendQueryObject qo) {
        return commendService.getCommendsByState(qo);
    }
    // 游记点赞开发判断当前用户是否登录
    @GetMapping("/isLogin")
    public Object isLogin(){
        Map<String,Object> map = new HashMap<String, Object>(); // 用map来封装需求数据
        User currentUser = UserContext.getCurrentUser();
        if (currentUser == null){
            map.put("state",0);
        }else{
            map.put("state",1);
        }
        return map;
    }
    // 游记点赞
    @PostMapping("like")
    public Object getCountAndState(Long travelId){
        Long userId = UserContext.getCurrentUser().getId();
        Map<String, Object> map = travelLikeService.checkNumByUserIdAndStrategyId(userId, travelId);
        return map;

    }
    // 判断当前用户是否对当前游记已点赞
    @GetMapping("/{travelId}/isLike")
    public Object isLike(@PathVariable Long travelId){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            Long userId = UserContext.getCurrentUser().getId();
            // 查询数据库是否已点赞
            int flag = travelLikeService.checkIsLikeByUserIdAndTravelId(userId,travelId);
            map.put("state",flag);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",0);
        }
        return map;
    }

}
